#include "usergame.h"

#define MIN_BALL_SIZE 1
#define MAX_BALL_SIZE 40
#define DEFAULT_BALL_SIZE 5

static uint8_t ball_size=DEFAULT_BALL_SIZE;
static int8_t ball_x = LCDWIDTH/2; //set the horizontal position to the middle of the screen
static int8_t ball_y = LCDHEIGHT/2; //vertical position
static int8_t ball_vx = 1; //horizontal velocity
static int8_t ball_vy = 1; //vertical velocity




void game1Start(uint8_t cmd)
{
		gb.display.drawRect(0,0,LCDWIDTH,15);
		gb.display.drawRoundRect(0,0,LCDWIDTH,15,4);
		gb.display.setFont(font5x7);
		gb.display.setCursor(16,4);   //看效果微调的
		gb.display.print(F("Move Ball"));
	
		gb.display.setCursor(25,25);
		gb.display.setFont(font3x5);
		gb.display.print(F("size :"));
		gb.display.print(ball_size);
		gb.display.setCursor(4,38);
		gb.display.setFont(font5x7);
		gb.display.print(F("please start!"));

		if((cmd==CMD_START)||(cmd==CMD_DOWN))
		{
			gb.gameState=GAME_STATE_RUN;
		}
		if(cmd==CMD_LEFT)
		{
			if(ball_size>MIN_BALL_SIZE)
			{
				ball_size--;
			}
		}
		else if(cmd==CMD_RIGHT)
		{
			if(ball_size<MAX_BALL_SIZE)
			{
				ball_size++;
			}
		}
		else if(cmd==CMD_NUMBER)
		{
			if((cache[0]>=MIN_BALL_SIZE)&&(cache[0]<=MAX_BALL_SIZE))
			{
				ball_size=cache[0];
			}
		}		
		ball_x = LCDWIDTH/2-ball_size/2;    //ball起始位置
		ball_y = LCDHEIGHT/2-ball_size/2; 
}
void game1Run(uint8_t cmd)
{
		//move the ball using the buttons
		if(cmd==CMD_UP)
		{ 
			ball_y = ball_y - ball_vy;	
		}
		if(cmd==CMD_DOWN)
		{
			ball_y = ball_y + ball_vy;					
		}
		if(cmd==CMD_LEFT)
		{
			ball_x = ball_x - ball_vx;			
		}
		if(cmd==CMD_RIGHT)
		{
			ball_x = ball_x + ball_vx; //increase the horizontal position by the ball's velocity		
		}	
		if(cmd==CMD_RETURN)
		{
			gb.gameState=GAME_STATE_RESTART;
		}
		if(cmd==CMD_NUMBER)
		{
			if((cache[0]>=MIN_BALL_SIZE)&&(cache[0]<=MAX_BALL_SIZE))
			{
				ball_size=cache[0];
			}
		}
		//check that the ball is not going out of the screen
		//if the ball is touching the left side of the screen
		if(ball_x < 0){
		//bring it back in the screen
		ball_x = 0;
		}
		//if the ball is touching the right side
		if((ball_x + ball_size) > LCDWIDTH){
		ball_x = LCDWIDTH - ball_size;
		}
		//if the ball is touching the top side
		if(ball_y < 0){
		ball_y = 0;
		}
		//if the ball is touching the down side
		if((ball_y + ball_size) > LCDHEIGHT){
		ball_y = LCDHEIGHT - ball_size;
		}
		//draw the ball on the screen
		gb.display.fillRect(ball_x, ball_y, ball_size, ball_size);	
}
void  game1Restart(uint8_t cmd)
{
		ball_x = LCDWIDTH/2-ball_size/2;    //ball起始位置
		ball_y = LCDHEIGHT/2-ball_size/2; 
		gb.gameState=GAME_STATE_START;
}


int8_t Game::game1()
{	
	gameState=GAME_STATE_START;
	display.clear();
	display.persistence=false;   //不保留每一次绘画内容
	if((gb.gameParameter1>=MIN_BALL_SIZE)&&(gb.gameParameter1<=MAX_BALL_SIZE))
	{
		ball_size=gb.gameParameter1;
	}
	else
	{
		ball_size=DEFAULT_BALL_SIZE;
	}
	while(1)
	{
		if(update())
		{
			uint8_t tmpCmd=user_i2cGetCmd();			
			if(state!=DIS_STATE_GAME1)
			{
				display.clear();
				display.update();
				return 1;
			}
			switch(gameState)
			{
				case GAME_STATE_START:
					game1Start(tmpCmd);
					break;
				case GAME_STATE_RUN:
					game1Run(tmpCmd);
					break;
//				case GAME_STATE_GAMEOVER:
//					popup(F("GAME OVER!"),50);
//					gameState=GAME_STATE_RESTART;
//					break;
				case GAME_STATE_RESTART:
					game1Restart(tmpCmd);
					break;
			}
		}
	}

}

